package com.egene.tw.activity.member;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.egene.tw.R;
import com.egene.tw.base.BaseActivity;
import com.egene.tw.network.ApiConstant;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_report_detail)
public class ReportDetailActivity extends BaseActivity {
    @Extra
    String reportName;
    @Extra
    String reportNo;

    //ToolBar Views
    @ViewById(R.id.actionBar)
    Toolbar mToolbarView;
    @ViewById(R.id.toolbar_title)
    TextView mToolBarTitle;

    @ViewById
    WebView wvWeb;

    SwipeRefreshLayout mSwipeRefresh;

    @AfterViews
    void initViews() {
        initToolBar();
//        http://114.119.7.10:9100/admin/order/app_category_result.aspx?orderid=8932D408D32441DFB358229F1FA64860
        wvWeb.loadUrl(ApiConstant.BASE_URL + "/admin/order/app_category_result.aspx?orderid=" + reportNo);
        wvWeb.getSettings().setJavaScriptEnabled(true);
        wvWeb.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mSwipeRefresh.setRefreshing(false);
            }
        });



        mSwipeRefresh = (SwipeRefreshLayout) findViewById(R.id.srl_refresh);
        mSwipeRefresh.setColorSchemeResources(R.color.main_green, R.color.main_gray, R.color.main_black, R.color.main_purple);
        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                wvWeb.loadUrl(ApiConstant.BASE_URL + "/admin/order/app_category_result.aspx?orderid=" + reportNo);
            }
        });
    }

    private void initToolBar() {
        mToolBarTitle.setText(R.string.check_report_result);
        mToolbarView.setNavigationIcon(R.drawable.ic_back);
        mToolbarView.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wvWeb.canGoBack()) {
                    wvWeb.goBack();// 返回前一个页面
                }else{
                    finish();
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && wvWeb.canGoBack()) {
            wvWeb.goBack();// 返回前一个页面
            return true;
        }else{
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
